package com.situ.company.employee.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.JDBCUtil;

public class EmployeeDao {

    public Integer insert(EmployeeModel model) {
        String sql = "insert into employee(code,name,pass,code_dept) values(?,?,?,?)";
        return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getPass(), model.getCodeDept());
    }

    public Integer delete(EmployeeModel model) {
        String sql = "delete from employee where code = ?";
        return JDBCUtil.update(sql, model.getCode());
    }

    public Integer update(EmployeeModel model) {
        String sql = "update employee set name=? where code=?";
        return JDBCUtil.update(sql, model.getName(), model.getCode());
    }

    public Integer updateDept(EmployeeModel model) {
        String sql = "update employee set code_dept=? where code=?";
        return JDBCUtil.update(sql, model.getCodeDept(), model.getCode());
    }

    public Integer updatePass(EmployeeModel model) {
        String sql = "update employee set pass=? where code=?";
        return JDBCUtil.update(sql, model.getPass(), model.getCode());
    }

    public Integer updateImage(EmployeeModel model) {
        String sql = "update employee set image=? where code=?";
        return JDBCUtil.update(sql, model.getImage(), model.getCode());
    }

    public List<EmployeeModel> select(EmployeeModel model) {
        StringBuffer sql = new StringBuffer(
                "select code,name,pass,code_dept,image,(select name from department where code=code_dept)nameDept from employee where 1=1 ");
        List<Object> vals = appendWhere(sql, model);
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        fields.put("pass", "pass");
        fields.put("codeDept", "code_dept");
        fields.put("nameDept", "nameDept");
        fields.put("image", "image");
        return JDBCUtil.query(sql.toString(), vals, EmployeeModel.class, fields);
    }

    private List<Object> appendWhere(StringBuffer sql, EmployeeModel model) {
        List<Object> list = new ArrayList<>();
        String code = model.getCode();
        if (code != null && !code.trim().isEmpty()) {
            sql.append(" and code like ? ");// and code like '计算机1'
            list.add(code);
        }
        String name = model.getName();
        if (name != null && !name.trim().isEmpty()) {
            sql.append(" and name like ? ");// and name like '%计算机%'
            list.add(name);
        }
        String codeDept = model.getCodeDept();
        if (codeDept != null) {
            sql.append(" and code_dept = ? ");
            list.add(codeDept);
        }
        return list;
    }

    public Integer updateActive(EmployeeModel model) {
        StringBuffer sql = new StringBuffer("update employee ");
        List<Object> list = appendSet(sql, model);
        sql.append(" where code=?");
        list.add(model.getCode());
        return JDBCUtil.update(sql.toString(), list);
    }

    private List<Object> appendSet(StringBuffer sql, EmployeeModel model) {
        List<Object> list = new ArrayList<>();
        sql.append(" set id=id ");
        String name = model.getName();
        if (name != null) {
            sql.append(" ,name = ? ");
            list.add(name);
        }
        String tel = model.getPass();
        if (tel != null) {
            sql.append(" ,pass = ? ");
            list.add(tel);
        }
        String codeDept = model.getCodeDept();
        if (codeDept != null) {
            sql.append(" ,code_dept = ? ");
            list.add(codeDept);
        }
        String image = model.getImage();
        if (image != null) {
            sql.append(" ,image = ? ");
            list.add(image);
        }
        return list;
    }

    public Integer selectCount(EmployeeModel model) {
        StringBuffer sql = new StringBuffer("select count(1) from employee where 1=1 ");
        List<Object> vals = appendWhere(sql, model);
        return JDBCUtil.queryInt(sql.toString(), vals);
    }

}
